package com.kgc.scd.controller;

import com.kgc.scd.annotation.RequestShopUser;
import com.kgc.scd.annotation.TokenPermission;
import com.kgc.scd.bean.ShopUser;
import com.kgc.scd.config.UserCollectConfig;
import com.kgc.scd.service.SignInServerService;
import com.kgc.scd.service.SignInWebService;
import com.kgc.scd.util.JudgingTimeUtil;
import com.kgc.scd.util.RedisUtils;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created On : 2022/11/16.
 * <p>
 * Author : yantao
 * <p>
 * Description: SignIn
 */
@Slf4j
@RestController
@RequestMapping("/yt")
@Api(tags = "10、用户签到操作入口(yt)",value = "用户每签到一次加2积分，连续签到7天加6积分，断签则重新记录连续签到天数")
public class SignInController {
    @Autowired
    private SignInServerService signInServerService;

    @Autowired
    private SignInWebService signInWebService;

    @Autowired
    private UserCollectConfig userCollectConfig;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private JudgingTimeUtil judgingTimeUtil;

    /**
     * @author : yantao
     * @date   : 2022/11/18
     * @param  : [shopUser, thisday]
     * @return : com.kgc.scd.util.RequestResult<?>
     * @description : doSignIn 
     */
    @GetMapping("doSignIn")
    @TokenPermission
    @ApiOperation(value = "进行签到", notes = "模拟签到的场景")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 999,message = "失败")
    })
    public RequestResult<?> doSignIn(@ApiIgnore @RequestShopUser ShopUser shopUser,@ApiParam(value = "模拟的签到的时间") @RequestParam String thisday) {

        if (ObjectUtils.isEmpty(redisUtils.get(userCollectConfig.getUserThisCheckInTimePrefix( ) + ":" + shopUser.getId( )))) {
            signInWebService.proceedSignIn(shopUser.getId( ),thisday);
            signInServerService.addDaySign(shopUser.getId());
            return ResultBuildUtil.success("签到成功");
        }

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date thisdate = null;
        try {
            //"2021-01-01 00:00:00"
            thisdate = df.parse(thisday);
        } catch (ParseException e) {
            e.printStackTrace( );
        }


        Date date2 = new Date( );
        date2.setTime(Long.parseLong(redisUtils.get(userCollectConfig.getUserThisCheckInTimePrefix( ) + ":" + shopUser.getId( )).toString( )));
        if (judgingTimeUtil.isSameDay(thisdate, date2)) {
            return ResultBuildUtil.fail("今天你已经签到过了");
        }
        signInServerService.addDaySign(shopUser.getId());
        signInWebService.proceedSignIn(shopUser.getId( ),thisday);
        return ResultBuildUtil.success("签到成功");
    }

    @GetMapping("upTime")
    @TokenPermission
    @ApiOperation(value = "更新时间", notes = "无参")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 999,message = "失败")
    })
    public RequestResult<?> upTime(@RequestParam String lastday) {

        signInWebService.updateTimeRedis(lastday);
        return ResultBuildUtil.success( );
    }

    @GetMapping("/inspectDays")
    @TokenPermission
    @ApiOperation(value = "查看连续签到天数", notes = "无参")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 999,message = "失败")
    })
    public RequestResult<?> inspectDays(@ApiIgnore @RequestShopUser ShopUser shopUser) {


        return ResultBuildUtil.success(signInWebService.examineDaysRedis(shopUser.getId()));
    }

    @GetMapping("/getLogonTime")
    @TokenPermission
    @ApiOperation(value = "查看登录的时间", notes = "无参")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 999,message = "失败")
    })
    public RequestResult<?> getLogonTime(@ApiIgnore @RequestShopUser ShopUser shopUser) {
        return ResultBuildUtil.success(signInServerService.getDayAll(shopUser.getId()));
    }



}
